package com.zxj.geekstudy.spring;

import lombok.Data;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Data
@Component
public class FruitProcessor implements BeanPostProcessor, BeanNameAware, ApplicationContextAware, BeanDefinitionRegistryPostProcessor {

    private String beanName;
    private ApplicationContext applicationContext;

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof Banana) {
            System.out.println("postProcessBeforeInitialization执行" + bean + beanName);
            Banana banana = (Banana) bean;
            System.out.println(banana.getName());
            banana.setName(banana.getName() + " 1元/斤");
        }
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof Banana) {
            System.out.println("postProcessAfterInitialization执行" + bean + beanName);
            Banana banana = (Banana) bean;
            System.out.println(banana.getName());
        }
        return bean;
    }

    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        System.out.println(" ==> postProcessBeanDefinitionRegistry前: " + registry.getBeanDefinitionCount());
//        System.out.println(" ==> postProcessBeanDefinitionRegistry: " + String.join(",", registry.getBeanDefinitionNames()));
        RootBeanDefinition rootBeanDefinition = new RootBeanDefinition(Banana.class);
        rootBeanDefinition.getPropertyValues().add("id", 101);
        rootBeanDefinition.getPropertyValues().add("name", "黄香蕉");
        rootBeanDefinition.setInitMethodName("init");// 需手动设置，否则不会执行
        registry.registerBeanDefinition("banana101", rootBeanDefinition);
        System.out.println(" ==> postProcessBeanDefinitionRegistry后: " + registry.getBeanDefinitionCount());
    }

    /**
     * 只添加到了spring容器中，但没有进行bean的增强操作
     *
     * @param beanFactory
     * @throws BeansException
     */
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        System.out.println(" ==> postProcessBeanFactory前: " + beanFactory.getBeanDefinitionCount());
//        System.out.println(" ==> postProcessBeanFactory: " + String.join(",", beanFactory.getBeanDefinitionNames()));
        beanFactory.registerSingleton("banana102", new Banana());
        System.out.println(" ==> postProcessBeanFactory后: " + beanFactory.getBeanDefinitionCount());
    }
}
